package com.cvsher.springtest.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cvsher.springtest.bean.User;
import com.cvsher.springtest.service.IUserService;

/**
 * 返回JSON字符串
 * REST方式
 * @author cvsher
 *
 */
@Controller
@RequestMapping("/users")
public class UserController {
	
	@Autowired
	private IUserService userService;

	@RequestMapping(value="", method=RequestMethod.GET)
	@ResponseBody
	public List<User> getAllUser(){
		
		return userService.getAllUser();
	}
	
	@RequestMapping(value="/{name}", method=RequestMethod.GET)
	@ResponseBody
	public User getUser(@PathVariable("name") String name, HttpServletRequest request){
		
		List<User> users = userService.getUsersByName(name);
		
		return (users==null || users.size()==0)?null:users.get(0);
	}
	
	@RequestMapping(value="/name/{name}", method=RequestMethod.GET)
	@ResponseBody
	public String getName(@PathVariable("name") String name){
		
		return name;
	}
	
}
